﻿using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace WinChatListen.Utils
{
    public class SubmitHtml
    {
        private string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-ms-application, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
        private CookieContainer cc = new CookieContainer();
        private string contentType = "application/json";
        private Cookie cookie = null;
        private HttpWebRequest httpWebRequest = null;
        private HttpWebResponse httpWebResponse = null;
        private Stream stream = null;
        private StreamReader streamReader = null;
        private static object submitHtmlLock = new object();
        private string user_agent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; CIBA; InfoPath.2; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";

        public Image GetImageFile(string url)
        {
            Image image = null;
            try
            {
                byte[] bytes = Encoding.Default.GetBytes(url);
                this.httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                this.httpWebRequest.Method = "POST";
                this.httpWebRequest.CookieContainer = this.cc;
                this.httpWebRequest.AllowAutoRedirect = true;
                this.httpWebRequest.Credentials = CredentialCache.DefaultNetworkCredentials;
                this.httpWebRequest.Timeout = 0x1388;
                this.httpWebRequest.ContentType = this.contentType;
                this.httpWebRequest.ContentLength = bytes.Length;
                this.httpWebRequest.Referer = url;
                this.httpWebRequest.Accept = this.accept;
                this.httpWebRequest.UserAgent = this.user_agent;
                this.httpWebRequest.KeepAlive = true;
                this.stream = this.httpWebRequest.GetRequestStream();
                this.stream.Write(bytes, 0, bytes.Length);
                this.httpWebResponse = (HttpWebResponse)this.httpWebRequest.GetResponse();
                this.stream = this.httpWebResponse.GetResponseStream();
                image = Image.FromStream(this.stream);
                if (this.cc.Count > 0)
                {
                    this.cookie = this.cc.GetCookies(new Uri(url))[0];
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                if (this.streamReader != null)
                {
                    this.streamReader.Close();
                    this.streamReader.Dispose();
                }
                if (this.stream != null)
                {
                    this.stream.Close();
                    this.stream.Dispose();
                }
            }
            return image;
        }


        public string Submit(string url, string strPost, SubmitMethod method)
        {
            string str = "";
            try
            {
                byte[] bytes = Encoding.Default.GetBytes(strPost);
                this.httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                this.httpWebRequest.Method = method.ToString();
                this.httpWebRequest.CookieContainer = this.cc;
                this.httpWebRequest.AllowAutoRedirect = true;
                this.httpWebRequest.Credentials = CredentialCache.DefaultNetworkCredentials;
                this.httpWebRequest.Timeout = 0x1388;
                this.httpWebRequest.ContentType = this.contentType;
                this.httpWebRequest.ContentLength = bytes.Length;
                this.httpWebRequest.Accept = this.accept;
                this.httpWebRequest.UserAgent = this.user_agent;
                this.httpWebRequest.KeepAlive = true;
                if (method == SubmitMethod.POST)
                {
                    this.stream = this.httpWebRequest.GetRequestStream();
                    this.stream.Write(bytes, 0, bytes.Length);
                    this.stream.Close();
                }
                this.httpWebResponse = (HttpWebResponse)this.httpWebRequest.GetResponse();
                this.stream = this.httpWebResponse.GetResponseStream();
                this.streamReader = new StreamReader(this.stream, Encoding.UTF8);
                str = this.streamReader.ReadToEnd();
                if ((this.cc.Count > 0) && (this.cc.GetCookies(new Uri(url)).Count > 0))
                {
                    this.cookie = this.cc.GetCookies(new Uri(url))[0];
                }
            }
            catch (TimeoutException)
            {
                str = "超时";
            }
            catch (WebException)
            {
            }
            finally
            {
                if (this.streamReader != null)
                {
                    this.streamReader.Close();
                    this.streamReader.Dispose();
                }
                if (this.stream != null)
                {
                    this.stream.Close();
                    this.stream.Dispose();
                }
            }
            return str;
        }

        public enum SubmitMethod
        {
            GET,
            POST
        }
    }

}
